package jp.co.ricoh.melody.views.fragments;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import jp.co.ricoh.melody.R;

/**
 * Copyright (C) 2016 RICOH Co.,LTD.
 * All rights reserved.
 * <p/>
 * This fragment is used to display sound picker.
 */
public class ProgressDialogFragment extends DialogFragment {
    private static final String ARG_PROGRESS_DIALOG_MSG = "ARG_PROGRESS_DIALOG_MSG";

    private TextView mDialogMsgTv;

    public static ProgressDialogFragment newInstance(String msg) {
        ProgressDialogFragment progressDialogFragment = new ProgressDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putString(ARG_PROGRESS_DIALOG_MSG, msg);
        progressDialogFragment.setArguments(bundle);
        return progressDialogFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_progress_dialog, container, false);
        mDialogMsgTv = (TextView) view.findViewById(R.id.progress_dialog_msg);
        if (!TextUtils.isEmpty(getArguments().getString(ARG_PROGRESS_DIALOG_MSG))) {
            mDialogMsgTv.setText(getArguments().getString(ARG_PROGRESS_DIALOG_MSG));
        }
        setCancelable(false);
        return view;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // Set dialog fragment without title.
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        // Set uncancel on touch outside.
        dialog.setCanceledOnTouchOutside(false);
        // Set background color of dialog.
        dialog.getWindow().setBackgroundDrawableResource(R.drawable.common_dialog_border);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        // Set size for dialog.
        if (dialog != null) {
            dialog.getWindow().setLayout((int) getResources().getDimension(R.dimen.dimen_progress_dialog_width),
                    (int) getResources().getDimension(R.dimen.dimen_progress_dialog_height));
        }
    }
}
